package com.hopes.bizz.poll.om.helper;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Pair;
import com.hopes.bizz.poll.common.constant.OmConstant;
import com.hopes.boss.common.core.exception.BizException;

import java.util.Date;

/**
 * @author lyb
 * @version 1.0
 * @description: 污染源数据时间解析
 * @date 2023/10/10 16:29
 */
public class PollDataTimeParse {

    public static Pair<Date, Date> Parse(Date date, Integer dataType) {
        Date beginTime;
        Date endTime;
        if (OmConstant.DATA_TYPE_RT.equals(dataType)) {
            beginTime = DateUtil.beginOfMinute(date);
            endTime = DateUtil.beginOfSecond(DateUtil.endOfMinute(beginTime));
        } else if (OmConstant.DATA_TYPE_MIN.equals(dataType)) {
            beginTime = DateUtil.beginOfMinute(date);
            endTime = DateUtil.beginOfSecond(DateUtil.offsetMinute(beginTime, 10));
        } else if (OmConstant.DATA_TYPE_HOUR.equals(dataType)) {
            beginTime = DateUtil.beginOfHour(date);
            endTime = DateUtil.beginOfSecond(DateUtil.endOfHour(beginTime));
        } else if (OmConstant.DATA_TYPE_DAY.equals(dataType)) {
            beginTime = DateUtil.beginOfDay(date);
            endTime = DateUtil.beginOfSecond(DateUtil.endOfDay(beginTime));
        } else if (OmConstant.DATA_TYPE_MONTH.equals(dataType)) {
            beginTime = DateUtil.beginOfMonth(date);
            endTime = DateUtil.beginOfSecond(DateUtil.endOfMonth(beginTime));
        } else if (OmConstant.DATA_TYPE_YEAR.equals(dataType)) {
            beginTime = DateUtil.beginOfYear(date);
            endTime = DateUtil.beginOfSecond(DateUtil.endOfYear(beginTime));
        } else {
            throw new BizException("污染源时间解析失败,无匹配解析数据类型");
        }
        return new Pair<>(beginTime, endTime);
    }


    public static String format(Date date, Integer dataType) {
        if (OmConstant.DATA_TYPE_RT.equals(dataType)) {
            return DateUtil.formatDateTime(date);
        } else if (OmConstant.DATA_TYPE_MIN.equals(dataType)) {
            return DateUtil.format(date, DatePattern.NORM_DATETIME_MINUTE_PATTERN);
        } else if (OmConstant.DATA_TYPE_HOUR.equals(dataType)) {
            return DateUtil.format(date, "yyyy-MM-dd HH");
        } else if (OmConstant.DATA_TYPE_DAY.equals(dataType)) {
            return DateUtil.formatDate(date);
        } else if (OmConstant.DATA_TYPE_MONTH.equals(dataType)) {
            return DateUtil.format(date, DatePattern.NORM_MONTH_PATTERN);
        } else if (OmConstant.DATA_TYPE_YEAR.equals(dataType)) {
            return DateUtil.format(date, DatePattern.NORM_YEAR_PATTERN);
        } else {
            throw new BizException("污染源时间解析失败,无匹配解析数据类型");
        }
    }
}
